// pages/videoDetail/videoDetail.js
var app = getApp();

var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        imageShow: true,
        fullScreen: false
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        //解析分享二维码和分享首页的数据scene
                var scene = decodeURIComponent(options.scene);
        var s_leader = scene.split(":");
        that.setData({
            options: options,
            s_leader: s_leader
        });
        //解析商品id  从页面点击传 id  二维码为scene的[3]
                var goodsId;
        if (options.id) {
            goodsId = options.id;
        } else {
            goodsId = s_leader[3];
        }
        that.setData({
            goodsId: goodsId
        });
        //首次直接进入，拿openid
                if (app.globalData.user) {
            //如果直接拿到openid
            that.requestStart(app.globalData.user.openid);
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.requestStart(res.openid);
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {
        this.videoContext = wx.createVideoContext("videos");
    },
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {
        this.pauseVideo();
    },
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        var details = that.data.details;
        var product_id = details.id;
        var my_leaderID = that.data.myleaderinfo.leader_id;
        var imgURL = that.data.imgURL;
        var sharePath = sharePath = "/pages/videoDetail/videoDetail?id=" + product_id + "&my_leaderID=" + my_leaderID;
        return {
            title: details.name,
            imageUrl: imgURL,
            path: sharePath
        };
    },
    noFunction() {},
    //页面请求开始
    requestStart: function(openid) {
        let that = this;
        that.getMyLeaderInfo(openid);
        //产品详情相关信息
                that.getData();
        that.getVideoSrc();
    },
    //团长信息精简版
    getMyLeaderInfo: function(openid) {
        let that = this;
        var openid = openid ? openid : wx.getStorageSync("user").openid;
        //用户数据
                common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    myleaderinfo: res.extra
                });
            }
        });
    },
    //获取商品信息
    getData: function() {
        var that = this;
        var id = that.data.goodsId;
        common.request({
            endpoint: "product/product_detail",
            method: "post",
            data: {
                id: id,
                visit_type: 1
            },
            success: res => {
                let details = res.extra;
                var headers = [];
                if (details.image.length > 6) {
                    headers = details.image.slice(0, 5);
                } else {
                    headers = details.image;
                }
                that.setData({
                    details: details,
                    headers: headers
                });
                that.getShareImg();
                if (res.code == 1) {
                    wx.showToast({
                        title: res.message,
                        icon: "none"
                    });
                }
            },
            fail(err) {
                if (err.code == 1) {
                    wx.showModal({
                        title: "提示",
                        content: err.message,
                        confirmText: "返回首页",
                        showCancel: false,
                        success() {
                            wx.reLaunch({
                                url: "/pages/index/index"
                            });
                        }
                    });
                }
            }
        });
    },
    //播放视频
    getVideoSrc() {
        let that = this;
        var id = that.data.goodsId;
        common.request({
            endpoint: "video/video_play",
            data: {
                id: id
            },
            method: "post",
            success(res) {
                that.setData({
                    video: res.data,
                    imageShow: false
                });
            },
            fail(err) {
                that.toastControl("video/video_play请求错误");
            }
        });
    },
    //提示框
    toastControl(content, time) {
        var that = this;
        that.setData({
            toastContent: content,
            toastHide: false
        });
        //显示提示框
                setTimeout(() => {
            that.setData({
                toastHide: true
            });
        }, !time ? 3e3 : time);
        //定时隐藏提示框,默认3秒后隐藏
        },
    //获取分享图
    getShareImg() {
        let that = this;
        var details = that.data.details;
        common.request({
            endpoint: "share/share_pro",
            method: "post",
            data: {
                id: details.id,
                //商品id
                price: details.low_price,
                //现价
                original_price: details.low_oprice,
                //原价
                sale: details.order_num,
                //已售份数
                least: details.all_stock_number
            },
            success(res) {
                res = JSON.parse(res);
                var imgURL = res.extra.image[0].url;
                if (res.code == 4) {
                    that.setData({
                        imgURL: that.data.details.icon
                    });
                } else {
                    that.setData({
                        imgURL: imgURL
                    });
                }
            },
            fail() {}
        });
    },
    //暂停视频
    pauseVideo() {
        this.videoContext.pause();
    },
    //视频错误
    videoErr(e) {
        this.toastControl("视频格式错误");
    },
    //屏幕横竖屏切换
    videoScreenChange(e) {
        this.setData({
            fullScreen: e.detail.fullScreen
        });
    },
    //提示框
    toastControl(content, time) {
        var that = this;
        that.setData({
            toastContent: content,
            toastHide: false
        });
        //显示提示框
                setTimeout(() => {
            that.setData({
                toastHide: true
            });
        }, !time ? 3e3 : time);
        //定时隐藏提示框,默认3秒后隐藏
        },
    toDetail() {
        wx.navigateTo({
            url: "/pages/pro_detail/pro_detail?id=" + this.data.goodsId
        });
    }
});